
//  Created by 莫瑞权 on 2022/1/17.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit

public class TalkfunHitTestView: UIView {
    //穿透
    public override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        
        if (self.isUserInteractionEnabled == false || self.isHidden == true || self.alpha <= 0.01 ) {
            return nil
        }

        
        // 2.判断下点在不在当前控件上
        // 用pointInside方法判断点在不在控件上,point这个必须是方法调用者坐标系上的点
        if (self.point(inside: point, with: event) == false){
            return nil
        }
        // 3.从后往前遍历自己的子控件
        
        let count = self.subviews.count
        
        //倒序
        for i in (0..<count).reversed() {

        // 取出子控件
        let childV = self.subviews[i] as UIView
            
        // 把自己坐标系上点转化成子控件坐标系上点
          let  childP =  self.convert(point, to: childV)
          let fitView =  childV.hitTest(childP, with: event)
             
            // 如果找到最合适view就直接返回
            if ((fitView) != nil) {
                return fitView;
            }
            
        }
        
// 方案二
//        let hitView = super.hitTest(point, with: event)
//
//        if(hitView == self){
//
//        return nil
//
//        }

        return nil

    }
}
